﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommandLine;
using CommandLine.Text;
using System.IO;
using System.Runtime.Serialization;

using Syncfusion.XlsIO;

namespace FileListBuilder
{
    class XLSWriter : IWriter
    {
        public void Write(Options options, List<Document> lstDocuments)
        {
            string sCompleteFileName = options.StartDirectory + "\\" + options.OutputFilename + "." + options.OutputFileFormat;
            Program.Log(0, "Start writing result file in " + sCompleteFileName, options);

            ExcelEngine excelEngine = new ExcelEngine();
            IApplication application = excelEngine.Excel;
            IWorkbook workbook = application.Workbooks.Create(1);
            IWorksheet sheet = workbook.Worksheets[0];

            iRow = 1;
            Write(sheet,options);
            foreach (Document item in lstDocuments)
            {
                Write(item,sheet,options);
            }

            for(int i=1;i<9;i++)
                sheet.AutofitColumn(i);

            try
            {
                // Saving the workbook to disk.
                workbook.SaveAs(sCompleteFileName);
                // Closing the workbook.
                workbook.Close();
                // Dispose the Excel engine.
                excelEngine.Dispose();
            }
            catch (Exception ex)
            {
                Program.Log(0, "EXCEPTION during Excel SaveAs: ", options);
                Program.Log(0, ex.Message + "\n" + ex.Source, options);
            }

            Program.Log(0, "End writing result file", options);
        }

        /// <summary>
        /// Ecriture d'une ligne de données
        /// </summary>
        /// <param name="item"></param>
        /// <param name="sheet"></param>
        private void Write(Document item,IWorksheet sheet,Options options)
        {
            try
            {
                string sAddress = item.Directory + "\\" + item.FileName;

                Program.Log(2, String.Format("Write Row {0}-[{1}] begin", iRow, sAddress), options);

                // Creating a Hyperlink for Opening Files using type as File.
                IHyperLink hyperlink2 = sheet.HyperLinks.Add(sheet.Range["A" + iRow.ToString()]);
                hyperlink2.Type = ExcelHyperLinkType.File;
                hyperlink2.Address = sAddress;
                hyperlink2.ScreenTip = item.FileName;
                hyperlink2.TextToDisplay = item.FileName;

                //$$$sheet.Range["A" + iRow.ToString()].Text = item.CompleteFileName;
                sheet.Range["B" + iRow.ToString()].Text = item.Directory;
                sheet.Range["C" + iRow.ToString()].Text = item.FileName;

                if (string.IsNullOrEmpty(item.Type) == false)
                    sheet.Range["D" + iRow.ToString()].Text = item.Type;

                if (string.IsNullOrEmpty(item.Service) == false)
                    sheet.Range["E" + iRow.ToString()].Text = item.Service;

                if (string.IsNullOrEmpty(item.Numero) == false)
                    sheet.Range["F" + iRow.ToString()].Text = item.Numero;

                if (string.IsNullOrEmpty(item.Version) == false)
                    sheet.Range["G" + iRow.ToString()].Text = item.Version;

                if (string.IsNullOrEmpty(item.Title) == false)
                    sheet.Range["H" + iRow.ToString()].Text = item.Title;

                Program.Log(2, String.Format("Write Row {0} end", iRow), options);
                iRow++;
            }
            catch (Exception ex)
            {
                Program.Log(0, "EXCEPTION in Write Row: ", options);
                Program.Log(0, ex.Message + "\n" + ex.Source, options);
            }
        }

        /// <summary>
        /// Ecriture du titre des colonne
        /// </summary>
        /// <param name="sheet"></param>
        private void Write(IWorksheet sheet, Options options)
        {
            try
            {
                Program.Log(2, "Write Column header begin", options);

                sheet.Range["A" + iRow.ToString()].CellStyle.Font.Bold = true;
                sheet.Range["A" + iRow.ToString()].Text = "CompleteFileName";
                sheet.Range["B" + iRow.ToString()].CellStyle.Font.Bold = true;
                sheet.Range["B" + iRow.ToString()].Text = "Directory";
                sheet.Range["C" + iRow.ToString()].CellStyle.Font.Bold = true;
                sheet.Range["C" + iRow.ToString()].Text = "FileName";
                sheet.Range["D" + iRow.ToString()].CellStyle.Font.Bold = true;
                sheet.Range["D" + iRow.ToString()].Text = "Type";
                sheet.Range["E" + iRow.ToString()].CellStyle.Font.Bold = true;
                sheet.Range["E" + iRow.ToString()].Text = "Service";
                sheet.Range["F" + iRow.ToString()].CellStyle.Font.Bold = true;
                sheet.Range["F" + iRow.ToString()].Text = "Numero";
                sheet.Range["G" + iRow.ToString()].CellStyle.Font.Bold = true;
                sheet.Range["G" + iRow.ToString()].Text = "Version";
                sheet.Range["H" + iRow.ToString()].CellStyle.Font.Bold = true;
                sheet.Range["H" + iRow.ToString()].Text = "Title";

                iRow++;
                Program.Log(2, "Write Column header end", options);
            }
            catch (Exception ex)
            {
                Program.Log(0, "EXCEPTION in Write column header : ", options);
                Program.Log(0, ex.Message+"\n"+ex.Source, options);
            }
        }

        private int iRow;
    }
}
